package com.guochuang.yunwang.promanager.config;

import com.guochuang.yunwang.promanager.interceptors.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {


    /**
     * 对拦截器实例化
     * @return
     */
    @Bean
    public LoginInterceptor getLoginInterceptor(){
        return new LoginInterceptor();
    }


    // 添加拦截器
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {

        // 这里直接调用上面的拦截器方法，然后容器会直接返回拦截器
        InterceptorRegistration interceptor = registry.addInterceptor(getLoginInterceptor());

        interceptor.addPathPatterns("/**")
                .excludePathPatterns("/doc.html","/swagger-resources","/swagger-resources/configuration/ui")
                .excludePathPatterns("/webjars/bycdao-ui/**");

        super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
